import 'package:elevator/db/db_helper.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../../../../model/build_info.dart';
import '../../../../../../model/lift_info.dart';
import '../../../../../../utils/eventbus.dart';

class StatusLogic extends GetxController {

  BuildInfo  buildInfo = BuildInfo();
  LiftInfo liftInfo = LiftInfo();

  final int ID_LIFT_INFO = 0x1002;

  late Function(dynamic) busCallBack;

  @override
  void onInit() {
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
    debugPrint("StatusLogic:onReady:liftInfo_${buildInfo.buildId}_${liftInfo.order}");
    busCallBack = (arg){
      liftInfo = arg;
      update([ID_LIFT_INFO]);
    };
    bus.on('${bus.liftInfo_Key}${buildInfo.buildId}_${liftInfo.order}', busCallBack);
  }

  reset(){
    if(buildInfo.buildId.isNotEmpty && liftInfo.order>=0){
      debugPrint("StatusLogic:reset:");
      bus.off('${bus.liftInfo_Key}${buildInfo.buildId}_${liftInfo.order}', busCallBack);
    }
  }

  @override
  void onClose() {
    debugPrint("StatusLogic:onClose:liftInfo_${buildInfo.buildId}_${liftInfo.order}");
    bus.off('${bus.liftInfo_Key}${buildInfo.buildId}_${liftInfo.order}', busCallBack);
    super.onClose();
  }

  String getImage() {
    if(0==liftInfo.currentState){
      return "images/lift_up.png";
    }else if(1==liftInfo.currentState){
      return "images/lift_down.png";
    }
    return "images/lift.png";
  }

}
